# 请假
from flask import Blueprint,request,jsonify,json
from flask.json import jsonify
from  flask_restful import Api,Resource
from settings.mysqlM import db
from settings.redisM import re


vaca=Blueprint('vaca',__name__)
api=Api(vaca)


class Vacation(Resource):
    def get(self):
        sql="select * from vacation"
        res=db.find_all(sql)
        return jsonify({'code':200,'list':res})


class Workflow(Resource):
    def get(self):
        sql="select * from wkflow"
        res=db.find_all(sql)
        return jsonify({'code':200,'list':res})

# api.add_resource(Workflow,'/work')




#获取数据源接口
class Getresourcedata(Resource):
# @bp_user.route('/getresourcedata')
    def get(self):
        #从redis取，如果有直接返回
        values = re.str_get('dataresouce')
        if values:
            list = eval(values)
        else:
            #如果没有查询
            sql = "select * from data_source"
            res = db.find_all(sql)
            list = []
            if res:
                for i in res:
                    dict={'id':i['id'],'name':i['name']}
                    sql1 = "select id,name,type from %s"%(i['tablement'])
                    subres = db.find_all(sql1)
                    dict['mes'] = subres
                    list.append(dict)
            #存入redis
            print(list)
            re.str_set('dataresouce',json.dumps(list))
        return jsonify({"code":200,'list':list})



api.add_resource(Workflow,'/work')
api.add_resource(Getresourcedata,'/getresourcedata')